package sample.test.cc.cool.gui;

import java.awt.Rectangle;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import sample.test.cc.cool.gui.controllers.AdmController;
import sample.test.cc.cool.gui.util.Containable;
import sample.test.cc.cool.gui.util.IntegerValidator;
import BESA.util.AddressHelper;

public class CreateContainnerWindow extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabel = null;
	private JComboBox ipAddressComboBox = null;
	private JLabel jLabel1 = null;
	private JTextField rmiPortField = null;
	private JLabel jLabel2 = null;
	private JTextField multicastAddressField = null;
	private JLabel jLabel3 = null;
	private JTextField multicastPortField = null;
	private JLabel jLabel4 = null;
	private JTextField aliasField = null;
	private JLabel jLabel5 = null;
	private JTextField gccPortField = null;
	private JButton cancelButton = null;
	private JButton createButton = null;

	private JFrame parent;
	
	/**
	 * This is the default constructor
	 */
	public CreateContainnerWindow(JFrame parent) {
		super();
		this.parent = parent;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(361, 357);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("eBESA - Create containner");
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setModal(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(45, 220, 91, 21));
			jLabel5.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel5.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel5.setText("GCC Port:");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(25, 20, 111, 21));
			jLabel4.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel4.setText("Containner Alias:");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(50, 180, 86, 21));
			jLabel3.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel3.setText("Multicast Port:");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(17, 140, 119, 21));
			jLabel2.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel2.setText("Multicast Address:");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(75, 100, 61, 21));
			jLabel1.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel1.setText("RMI Port:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(75, 60, 61, 21));
			jLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel.setText("IP:");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getIpAddressComboBox(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getRmiPortField(), null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getMulticastAddressField(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(getMulticastPortField(), null);
			jContentPane.add(jLabel4, null);
			jContentPane.add(getAliasField(), null);
			jContentPane.add(jLabel5, null);
			jContentPane.add(getGccPortField(), null);
			jContentPane.add(getCancelButton(), null);
			jContentPane.add(getCreateButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes ipAddressComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getIpAddressComboBox() {
		if (ipAddressComboBox == null) {
			ipAddressComboBox = new JComboBox(AddressHelper.getLocalAddressV4()
					.toArray());
			ipAddressComboBox.setBounds(new Rectangle(150, 60, 166, 21));
		}
		return ipAddressComboBox;
	}

	/**
	 * This method initializes rmiPortField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getRmiPortField() {
		if (rmiPortField == null) {
			rmiPortField = new JTextField();
			rmiPortField.setInputVerifier(new IntegerValidator());
			rmiPortField.setText("1099");
			rmiPortField.setBounds(new Rectangle(150, 100, 166, 21));
		}
		return rmiPortField;
	}

	/**
	 * This method initializes multicastAddressField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMulticastAddressField() {
		if (multicastAddressField == null) {
			multicastAddressField = new JTextField();
			multicastAddressField.setBounds(new Rectangle(150, 140, 166, 21));
			multicastAddressField.setText("230.0.0.1");
		}
		return multicastAddressField;
	}

	/**
	 * This method initializes multicastPortField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getMulticastPortField() {
		if (multicastPortField == null) {
			multicastPortField = new JTextField();
			multicastPortField.setInputVerifier(new IntegerValidator());
			multicastPortField.setText("10001");
			multicastPortField.setBounds(new Rectangle(150, 180, 166, 21));
		}
		return multicastPortField;
	}

	/**
	 * This method initializes aliasField
	 * s
	 * @return javax.swing.JTextField
	 */
	private JTextField getAliasField() {
		if (aliasField == null) {
			aliasField = new JTextField();
			aliasField.setBounds(new Rectangle(150, 20, 166, 21));
		}
		return aliasField;
	}

	/**
	 * This method initializes gccPortField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getGccPortField() {
		if (gccPortField == null) {
			gccPortField = new JTextField();
			gccPortField.setInputVerifier(new IntegerValidator());
			gccPortField.setText("10003");
			gccPortField.setBounds(new Rectangle(150, 220, 166, 21));
		}
		return gccPortField;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setBounds(new Rectangle(220, 270, 91, 31));
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
					parent.dispose();
				}
			});
		}
		return cancelButton;
	}

	/**
	 * This method initializes createButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCreateButton() {
		if (createButton == null) {
			createButton = new JButton();
			createButton.setBounds(new Rectangle(120, 270, 91, 31));
			createButton.setText("Create");
			createButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (checkField()) {
						AdmController.getInstance().createAdmin(
								aliasField.getText(),
								(String) ipAddressComboBox.getSelectedItem(),
								Integer.parseInt(rmiPortField.getText()),
								multicastAddressField.getText(),
								Integer.parseInt(multicastPortField.getText()),
								Integer.parseInt(gccPortField.getText()));
						Containable c = (Containable)parent;
						c.updateContainnerMetaData();
						dispose();
					}
				}
			});
		}
		return createButton;
	}

	private boolean checkField() {

		String alias = aliasField.getText();
		String mcA = multicastAddressField.getText();
		int rmiPort;
		int mcP;
		int gccP;
		try {
			rmiPort = Integer.parseInt(rmiPortField.getText());

			mcP = Integer.parseInt(multicastPortField.getText());
			gccP = Integer.parseInt(gccPortField.getText());
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, "Ports must be numbers",
					"Error", JOptionPane.ERROR_MESSAGE);
			return false;
		}

		if (alias.length() < 1 && !alias.matches("[A-Za-z_-\\.]+")) {
			JOptionPane.showMessageDialog(this, "Invalid containner name",
					"Error", JOptionPane.ERROR_MESSAGE);
			return false;
		} else if (rmiPort < 0 && rmiPort > 65535 && mcP < 0 && mcP > 65535
				&& gccP < 0 && gccP > 65535) {
			JOptionPane.showMessageDialog(this,
					"One or more port number are out of range", "Error",
					JOptionPane.ERROR_MESSAGE);
			return false;
		} else if (rmiPort == mcP && gccP == rmiPort && mcP == gccP) {
			JOptionPane.showMessageDialog(this, "Port number must be unique",
					"Error", JOptionPane.ERROR_MESSAGE);
			return false;
		} else {
			try {
				InetAddress a = InetAddress.getByName(mcA);
				if (!a.isMulticastAddress())
					throw new UnknownHostException();
			} catch (UnknownHostException e) {
				JOptionPane.showMessageDialog(this,
						"Not a valid multicast address", "Error",
						JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
		return true;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
